package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import com.itheima.domain.db.RecommendVideo;
import com.itheima.domain.db.Video;
import com.itheima.service.mongo.VideoService;
import com.itheima.util.ConstantUtil;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zsk
 * @create 2022-03-23-15:54
 */
@DubboService
public class VideoServiceImpl implements VideoService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private RedisIdService redisIdService;


    //分页查询小视频列表
    @Override
    public PageBeanVo findVideoByPage(Long userId, Integer pageNum, Integer pageSize) {

        //构建查询条件
        Query query = new Query(Criteria.where("userId").is(userId))
                .with(Sort.by(Sort.Order.desc("date")))//按照时间降序
                .with(Sort.by(Sort.Order.desc("score")))//按照评分降序
                .skip((pageNum -1) * pageSize).limit(pageSize);//分页

        //查询
        List<RecommendVideo> recommendVideoList = mongoTemplate.find(query, RecommendVideo.class);

        //创建一个Video集合，用来存储Video数据
        List<Video> videoList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(recommendVideoList)){
            for (RecommendVideo recommendVideo : recommendVideoList) {
                //查询详情
                Video video = mongoTemplate.findById(recommendVideo.getVideoId(), Video.class);
                videoList.add(video);
            }
        }
        //返回并封装分页对象
        return new PageBeanVo(pageNum,pageSize,0L,videoList);
    }

    //发布视频
    @Override
    public void saveVideo(Video video) {
        //设置视频的Vid
        Long vid = redisIdService.getNextId(ConstantUtil.VIDEO_ID);
        video.setVid(vid);

        //保存视频
        mongoTemplate.save(video);

        //将此视频推荐给自己
        RecommendVideo recommendVideo = new RecommendVideo();
        recommendVideo.setDate(System.currentTimeMillis());//时间
        recommendVideo.setVid(vid);//大数据id
        recommendVideo.setUserId(video.getUserId());//推荐给的用户id
        recommendVideo.setVideoId(video.getId());//视频id
        recommendVideo.setScore(RandomUtil.randomDouble(60,90));//推荐得分
        //保存
        mongoTemplate.save(recommendVideo);

    }

    @Override
    public PageBeanVo findByUserVideos(Long userId, Integer pageNum, Integer pageSize) {

        //构建查询条件
        Query query = new Query(
                Criteria.where("userId").is(userId))
                .with(Sort.by(Sort.Order.desc("date")))//按照发布时间降序
                .skip((pageNum - 1) * pageSize).limit(pageSize);//分页

        //查询
        List<Video> videoList = mongoTemplate.find(query, Video.class);

        //查询个数
        long count = mongoTemplate.count(query, Video.class);

        //返回结果
        return new PageBeanVo(pageNum,pageSize,count,videoList);
    }
}
